import {Component, OnInit} from '@angular/core';
import {Events, ModalController, NavController, ToastController} from '@ionic/angular';
import {BasePage} from '../../common/basePage';
import {ActivatedRoute} from '@angular/router';
import {HttpService} from '../../services/http.service';
import {UrlAndOPT} from '../../common/url-and-opt.enum';
import {IframePageComponent} from '../../components/iframe-page/iframe-page.component';
import {AppConfig} from '../../common/app-config.enum';
import {Storage} from '@ionic/storage';

/**
 * 充值
 */
@Component({
  selector: 'app-user-recharge',
  templateUrl: './user-recharge.page.html',
  styleUrls: ['./user-recharge.page.scss'],
})
export class UserRechargePage extends BasePage implements OnInit {

  amount: any = null;
  errMsg = '';
  userRole: string;
  rechargeHighest: number;
  rechargeLowest: number;

  constructor(public navCtrl: NavController,
              public toastCtrl: ToastController,
              private storage: Storage,
              private modalCtrl: ModalController,
              private events: Events,
              private routeInfo: ActivatedRoute,
              private service$: HttpService
  ) {
    super(navCtrl, toastCtrl);
  }

  ngOnInit() {
    this.getData();
  }

  // 充值准备
  getData() {
    this.storage.get(AppConfig.USER_INFO)
      .then(userInfo => {
        this.userRole = userInfo[AppConfig.USER_ROLE_KEY];
        this.service$.doRequest(UrlAndOPT.auth_recharge_pre, {userRole: this.userRole})
          .subscribe(obj => {
            if (obj) {
              this.rechargeHighest = obj['rechargeHighest'] * 1;
              this.rechargeLowest =  obj['rechargeLowest'] * 1;
              this.errMsg = '单笔最低充值金额为' + this.rechargeLowest + '元';
            }
          });
      });
  }

  // 限制输入：最多保留两位小数，如果超出金额限制显示相关提示信息
  limitValue() {
    if (this.amount) {
      this.amount = Math.floor(this.amount * 100) / 100;
    } else {
      this.amount = null;
    }
    if (!this.amount || this.amount < this.rechargeLowest) {
      this.errMsg = '单笔最低充值金额为' + this.rechargeLowest + '元';
    } else if (this.amount > this.rechargeHighest) {
      this.errMsg = '单笔最高充值金额为' + this.rechargeHighest + '元';
    } else {
      this.errMsg = '';
    }
  }

  // 前往银行充值页面
  async doRecharge() {
    if (!this.errMsg && this.amount) {
      const params: any = {amount: this.amount, userRole: this.userRole};
      this.service$.doRequest(UrlAndOPT.user_recharge, params)
        .subscribe(async (obj) => {
          if (obj) {
            // 打开充值model
            const modal = await this.modalCtrl.create({
              component: IframePageComponent,
              componentProps: {
                title: '充值',
                url: obj['url']
              }
            });
            await modal.present();
            // 关闭model后，发送用户账户变动通知，关闭充值页面
            await modal.onDidDismiss();
            this.events.publish(AppConfig.TOPIC_USER_FUNC);
            this.navCtrl.navigateBack(['/tabs/user']);
          }
        });
    }
  }

}
